1581B - Diameter of Graph - CodeForces Solution


constructive algorithms graphs greedy math *1200

Please click on ads to support us..

Python Code:

def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        n, m, k = readIntArr()
        
        k -= 2          lo = n - 1
        hi = (n * (n - 1)) // 2
        if not lo <= m <= hi:
            allans.append('NO')
            continue
        
        if n == 1:
            if k >= 0:
                allans.append('YES')
            else:
                allans.append('NO')
            continue
        
        if n == 2:
            if k >= 1:
                allans.append('YES')
            else:
                allans.append('NO')
            continue
                if k <= 0:
            ans = 'NO'
        elif k == 1:
            if m == hi:
                ans = 'YES'
            else:
                ans = 'NO'
        else:
                        ans = 'YES'
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

// author Vardhan Gacche
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define read(a, n)              \
    for (int i = 0; i < n; i++) \
        cin >> a[i];
#define show(a)           \
    for (auto x : a)      \
        cout << x << " "; \
    cout << endl;

#define sort(a) \
    sort(a.begin(), a.end());
char get(int i)
{
    return 'a' + i - 1;
}
int returnVal(char x)
{
    return (int)x - 96;
}
int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int lcm(int a, int b)
{
    return (a * b) / gcd(a, b);
}

void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (name.size())
    {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

void view(vector<vector<char>> a)
{
    for (vector<char> x : a)
    {
        show(x);
    }
}

int main()
{
    // setIO("reduce");
    int t;
    cin >> t;
    while (t--)
    {
        long long int n, m, k, count = 0, x = 0;
        cin >> n >> m >> k;
        x = (n * (n - 1)) / 2;
        if(m < n - 1 || m > x)
        {
            no;
        }
        else if(n == 1)
        {
            if(k > 1 && m <= 0)
            {
                yes;
            }
            else
            {
                no;
            }
        }
        else if(m < x)
        {
            if(k > 3)
            {
                yes;
            }
            else
            {
                no;
            }
        }
        else    
        {
            if(k > 2)
            {
                yes;
            }
            else
            {
                no;
            }
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately